'use strict'

module.exports = () => {
  return async function Interceptor(ctx, next) {
    let authToken = ctx.headers.authorization
    // console.log('--------ctx--------', ctx.request)
    if (authToken) {
      authToken = authToken.substr(7)
      const verifyRes = await ctx.service.accessToken.verifyToken(authToken)
      if (verifyRes) { // 校验通过
        await next()
      } else {
        ctx.body = {
          code: 4001,
          msg: '登录状态已过期'
        }
      }
    } else {
      ctx.body = {
        code: 4002,
        msg: '请登录后再进行操作'
      }
    }
  }
}